package com.ssii.www.myproject.aop;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Aspect
@Component
/**
 *  日志记录了操作和service层操作的错误
 *  应该分别一个日志记录操作，一个日志记录错误信息 错误信息在方法执行前后try catch到异常进行记录
 *  这个日志就不try catch 也不抛出异常
 */
public class TxManager {

  //private static final Logger systemLogger = Logger.getLogger(TxManager.class);

  private static final Logger operateLogger = Logger.getLogger(TxManager.class);

  //切入点 拦截所有service下面以set开始的方法
  @Pointcut("execution(* com.ssii.www.myproject.service.*Service*.set*(..))")
  public void pointcut() {
  }

  @Around("pointcut()")
  public Object operate(ProceedingJoinPoint joinPoint) throws Throwable{
    Object proceed = null;
    Throwable throwable = null;

    Object[] args = joinPoint.getArgs();
    operateLogger.error("用户操作，方法为" + joinPoint.getSignature());
    for (Object arg : args) {
      //System.out.println("参数为"+arg);
      operateLogger.error("参数为" + arg);
    }
    try {
      proceed = joinPoint.proceed();
    } catch (Throwable throwable1) {
      throwable = throwable1;
      operateLogger.error("错误为"+throwable.getMessage());
    }

    if(throwable!=null)
      throw throwable;

    return proceed;
  }

}
